package com.tqyan.model.dto.paper;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 大纲
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @date 2023/4/10 16:55
 */
@Data
public class OutlineDTO implements Serializable {
    private static final long serialVersionUID = -8083195537494174034L;

    /** 论文标题 */
    private List<PaperTitleDTO> titleList;
    /** 论文翻译 */
    private PaperTranslateDTO translate;

    public OutlineDTO() {
        this.titleList = new ArrayList<>();
    }

    public void addNode(String node, String title) {
        titleList.add(new PaperTitleDTO(node, title));
    }

    public void addNode(String node, String title, List<ConvertOutlineDTO> subTitleList) {
        PaperTitleDTO paperTitle = new PaperTitleDTO(node, title);
        paperTitle.setSubTitle(new ArrayList<>());
        for (int i = 0; i < subTitleList.size(); i++) {
            ConvertOutlineDTO subTitle = subTitleList.get(i);
            PaperTitleDTO firstSubTitle = new PaperTitleDTO((i + 1) + "", subTitle.getFirst());
            paperTitle.getSubTitle().add(firstSubTitle);
            for (int j = 0; j < subTitle.getSecondList().size(); j++) {
                firstSubTitle.getSubTitle().add(new PaperTitleDTO(
                        (i + 1) + "." + (j + 1)
                        , subTitle.getSecondList().get(j)));
            }
        }
        titleList.add(paperTitle);
    }

    /**
     * @description 处理大纲
     * <p> <功能详细描述> </p>
     *
     * @author 陈晨
     * @date 2023/5/15 21:50
     */
    public static OutlineDTO handleOutline(List<ConvertOutlineDTO> outlineList) {
        // 大纲内容处理
        for (int i = outlineList.size() - 1; i >= 0; i--) {
            // 移除没有子标题的大纲
            ConvertOutlineDTO outline = outlineList.get(i);
            if (StringUtils.isBlank(outline.getFirst())
                    || CollectionUtils.isEmpty(outline.getSecondList())) {
                outlineList.remove(i);
            }
            // 移除引言、总结
            if (outline.getFirst().contains("引言")
                    || outline.getFirst().contains("前言")
                    || outline.getFirst().contains("导言")
                    || outline.getFirst().contains("总结")
                    || outline.getFirst().contains("结论")) {
                outlineList.remove(i);
            }
            outline.setFirst(handleTitle(outline.getFirst()));

            // 处理子标题
            List<String> seconds = new ArrayList<>();
            for (String second : outline.getSecondList()) {
                if (StringUtils.isBlank(second)) {
                    continue;
                }
                seconds.add(handleTitle(second));
            }
            outline.setSecondList(seconds);
        }

        // 设置响应
        OutlineDTO result = new OutlineDTO();
        // 一、问题的提出
        result.addNode("一", "问题的提出");
        // 二、文献综述
        result.addNode("二", "文献综述");
        // 三、相关概念的界定
        result.addNode("三", "相关概念的界定");
        // 四、研究方法
        result.addNode("四", "研究方法", outlineList);
        // 五、结果与讨论、分析
        result.addNode("五", "结果与讨论、分析");
        // 六、建议与研究不足之处
        result.addNode("六", "建议与研究不足之处");
        return result;
    }

    /**
     * @description 处理标题
     * <p> <功能详细描述> </p>
     *
     * @author 陈晨
     * @date 2023/5/17 23:58
     */
    private static String handleTitle(String title) {
        if (StringUtils.isBlank(title)) {
            return title;
        }
        // 移除标题前缀
        title = title.replaceAll("：", ":")
                .replaceAll("，", ",")
                .trim();
        if (title.contains(":")) {
            title = title.split(":")[1];
        }
        if (title.contains("、")) {
            title = title.split("、")[1];
        }
        if (title.contains(",")) {
            title = title.split(",")[1];
        }
        if (title.contains(" ")) {
            title = title.split(" ")[1];
        }
        if (title.contains(".")) {
            title = title.split("\\.")[1];
        }
        // 移除末尾"。"
        if (title.contains("。")) {
            title = title.split("。")[0];
        }
        return title;
    }

}


